/* Author:

*/

$(document).ready(function() { 
    
    console.log( );
    
    
    $('div.percent-bar span').each(function(e){
       percent = $(this).data('percent');
       $(this).css({'width' : percent + '%'}); 
       if ( percent > 0 && percent <= 45 ) {
           $(this).addClass('percent_45');
       } else if ( percent > 45 && percent <= 60 ) {
           $(this).addClass('percent_60');
       } else if ( percent > 60 && percent <= 75 ) {
           $(this).addClass('percent_75');
       } else if ( percent > 75 && percent <= 90 ) {
           $(this).addClass('percent_90');
       } else if ( percent > 90 && percent <= 100 ) {
           $(this).addClass('percent_100');
       }
    });
    
    



    $('#advanced-search').hide();
    
    $('.toggle-advanced-search').click(function(e){
        e.preventDefault();
        $('#advanced-search').toggle();
    });
    
    $('.toggle-allowance-controls').click(function(e){
        e.preventDefault();
        $( $(this).attr('href') ).slideToggle();
    });  
    $('.toggle-single-allowance-controls').click(function(e){
        e.preventDefault();
        $(this).next('.single-allowance-controls').slideToggle();
    });  
    
    
    $('#add-toggle, #pause-toggle, #remove-toggle, #end-toggle').click(function(e){
       e.preventDefault();
       $(this).closest('ul').children('li').removeClass('active');
       $(this).parent('li').addClass('active');
       $('#user-add-allowance-frame form').hide();
       target = $(this).attr('href');
       $(target).show();
        
    });
    
    
    var pause_max_weeks = $( "#pause-datepicker" ).data('maxdate');
    
    $( "#pause-datepicker" ).datepicker({
        dateFormat: 'yy-mm-dd',
        constrainInput: false,
        minDate: +1,
        maxDate: pause_max_weeks * 7,
        beforeShowDay: function(date) {
            if (date.getDay() != setting_publication_day ) {
                return [false, ''];
            } else {
                return [true, ''];
            }
        //var day = date.getDay();
        //return [(day != 0 && day != 1 && day != 3 && day != 4 && day != 5 && day != 6)];
    }
    });
    
    $( "input.datepicker" ).datepicker({
        dateFormat: 'yy-mm-dd',
        constrainInput: false,
        minDate: +1,
        beforeShowDay: function(date) {
            if (date.getDay() != setting_publication_day ) {
                return [false, ''];
            } else {
                return [true, ''];
            }
        //var day = date.getDay();
        //return [(day != 0 && day != 1 && day != 3 && day != 4 && day != 5 && day != 6)];
    }
    });
    
    $( "input.full-datepicker" ).datepicker({
        dateFormat: 'yy-mm-dd',
        constrainInput: false
    });
    
    
    
    
    /**
     * 
     * 
     * Autocomplete anywhere where we are taking user name inputs
     */
    if( typeof user_names_autocomplete != 'undefined' ) {
    $( "#user-autocomplete, #user-autosort" ).autocomplete({
            source: user_names_autocomplete
    });
    }
    
    /**
     * 
     * 
     * Autosort users when typing in the sort box.
     */       
    $( "#user-autosort" ).keyup(function(){
        var value = $(this).val().toLowerCase();
        
        $(this).closest('.subscription-table').find('tbody tr td.table-displayname').each(function(){
           // Test against value
           // hide if true, reveal if false
           var html = $(this).data('displayname').toLowerCase();
           
           if ( html.indexOf(value) != -1) {
               $(this).closest('tr').show();
           } else {
               $(this).closest('tr').hide();
           }
       });
        
     });   
     
        





    /**
     * 
     * 
     * 
     * 
     * Tooltips!
     */
    $('.show-tool-tip').mouseenter(function(e) {
        // Disable the default tooltip.
        e.preventDefault();
        var $this = $(this);
        $this.attr('tt', $this.attr('title'))
                   .removeAttr('title');
                   
        // Store our timeout function in .data
        $this.data("hover_delay", setTimeout(function(title) {
            $('<aside class="tool-tip"><p>'+$this.attr('tt')+'</p></aside>').hide()
                .appendTo( $this )
                .css( {
                    'top' : (e.pageY + 16) + "px",
                    'left' : (e.pageX - 40) + "px"
            }
            ).fadeIn(500);
        }
        , 1600));
    }
    ).mouseleave(function() {
        var hover_delay = $(this).data("hover_delay");
        if (hover_delay) {
            clearTimeout(hover_delay);
            $(this).attr('title', $(this).attr('tt'));
            $(this).children('.tool-tip').fadeOut();
        }
    }
    );




    $('.accordion-item .accordion-title').click(function(e){
        e.preventDefault();
       $(this).siblings('.accordion-content').slideToggle(200).closest('.accordion-item').toggleClass('active'); 
       
       if ( $(this).children('#map-canvas').length > 0 ) { 
           initialize_map( $('#map-canvas') );
       }
       
       
    });



}); // End docready


